#include "stdafx.h"
#include "WinWindow.h"

CPlatformWindow::CPlatformWindow()
: m_handle(0)
, m_fullscreen(false)
{
	
}

bool CPlatformWindow::Init(XINSTANCE appHandle, void* pMsgProc)
{
	//	declare window class
	WNDCLASSEX wc;
	wc.cbClsExtra = 0;
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.cbWndExtra = 0;
	wc.hbrBackground = 0;//(HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
	wc.hInstance = static_cast<HINSTANCE>(appHandle);
	wc.hIcon = LoadIcon(wc.hInstance, MAKEINTRESOURCE(101));
	wc.hIconSm = 0;
	wc.lpfnWndProc = static_cast<WNDPROC>(pMsgProc);
	wc.lpszClassName = "XerX_window_class";
	wc.lpszMenuName = 0;
	wc.style = CS_HREDRAW | CS_VREDRAW;

	//	register window class
	if (!RegisterClassEx(&wc))
		return false;

	//	create window
	m_handle = CreateWindow(wc.lpszClassName, "startup in progress...", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, nullptr, nullptr, wc.hInstance, nullptr);
	if (!m_handle)
		return false;

	ShowWindow(m_handle, SW_SHOW);

	return true;
}

void CPlatformWindow::SetCaption(const char* caption)
{
	SetWindowText(m_handle, caption);
}

void CPlatformWindow::SetSize(uint width, uint height)
{
	MoveWindow(m_handle, GetXPos(), GetYPos(), width, height, true);
}

void CPlatformWindow::SetPos(int x, int y)
{
	MoveWindow(m_handle, x, y, GetWidth(), GetHeight(), false);
}

uint CPlatformWindow::GetClientWidth() const
{
	RECT rect;
	GetClientRect(m_handle, &rect);
	return rect.right - rect.left;
}

uint CPlatformWindow::GetClientHeight() const
{
	RECT rect;
	GetClientRect(m_handle, &rect);
	return rect.bottom - rect.top;
}

XWND CPlatformWindow::GetHandle() const
{
	return m_handle;
}

void CPlatformWindow::CenterScreen()
{
	//	calc window position to fit it to screen center
	uint  hres = XApi::GetScreenWidth();
	uint  vres = XApi::GetScreenHeight();
	SetPos((hres - GetWidth())/2, (vres - GetHeight())/2);
}

void CPlatformWindow::ToggleFullscreen()
{
	m_fullscreen = !m_fullscreen;
	
	int nWndStyle = m_fullscreen ? WS_POPUPWINDOW :	WS_OVERLAPPEDWINDOW;
	SetWindowLong(m_handle, GWL_STYLE, nWndStyle);

	static int x, y, width, height;
	if (m_fullscreen)
	{
		x = GetXPos();
		y = GetYPos();
		width = GetWidth();
		height = GetHeight();
		SetWindowPos(m_handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
		Maximize();
	}
	else
	{
		SetWindowPos(m_handle, HWND_NOTOPMOST, x, y, width, height, SWP_NOZORDER | SWP_SHOWWINDOW | SWP_FRAMECHANGED);
	}
}

void CPlatformWindow::Maximize()
{
	ShowWindow(m_handle, SW_MAXIMIZE);
}

void CPlatformWindow::Minimize()
{
	ShowWindow(m_handle, SW_MINIMIZE);
}

void CPlatformWindow::Close()
{
	CloseWindow(m_handle);
}

int CPlatformWindow::GetXPos() const
{
	RECT rect;
	GetWindowRect(m_handle, &rect);
	return rect.left;
}

int CPlatformWindow::GetYPos() const
{
	RECT rect;
	GetWindowRect(m_handle, &rect);
	return rect.top;
}

uint CPlatformWindow::GetWidth() const
{
	RECT rect;
	GetWindowRect(m_handle, &rect);
	return rect.right - rect.left;
}

uint CPlatformWindow::GetHeight() const
{
	RECT rect;
	GetWindowRect(m_handle, &rect);
	return rect.bottom - rect.top;
}